<template>
  <el-dialog :visible.sync="dialogVisible" width="800px" :before-close="handleClose" :close-on-click-modal="false"
    append-to-body :title="title">
    <el-form :model="form" ref="formRef" label-width="90px">
      <el-form-item label="角色" prop="role" :rules="[{ required: true, message: '请选择角色', trigger: 'blur' }]">
        <el-select v-model="form.role" clearable filterable placeholder="请选择">
          <el-option v-for="item in roleArr" :key="item.code" :label="item.name" :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="科室" prop="department" :rules="[{ required: true, message: '请输入科室', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.department"></el-input>
      </el-form-item>
      <el-form-item label="工号" prop="code" :rules="[{ required: true, message: '请输入工号', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '请输入姓名', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.name"></el-input>
      </el-form-item>

      <!-- <el-form-item
        label="登录账号"
        prop="username"
        :rules="[
          { required: true, message: '请输入登录账号', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.username"></el-input>
      </el-form-item> -->
      <el-form-item v-if="title == '添加人员'" label="登录密码" prop="name" :rules="[
          { required: true, message: '请输入登录密码', trigger: 'blur' },
        ]">
        <el-input placeholder="请输入" v-model="form.password"></el-input>
      </el-form-item>

      <el-form-item label="职称" prop="level" :rules="[{ required: true, message: '请输入职称', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.level"></el-input>

        <!--  <el-input placeholder="请输入" v-model="form.level"></el-input> -->
      </el-form-item>

      <!-- <el-form-item label="登录密码" prop="password" :rules="[{ required: true, message: '请输入登录密码', trigger: 'blur' }]">
        <el-input
          placeholder="请输入"
          v-model="form.password"
        ></el-input>
      </el-form-item> -->

      <el-form-item label="头像" prop="icon">
        <!--  {{ imgsDefalut }} -->
        <ImgUploadPath ref="ImgUploadPath" :limit="1" @success="onUploadSuccess" @remove="onUploadRemove"
          :imgsDefalut="imgsDefalut" />
      </el-form-item>

      <el-form-item label="手机号" prop="phone" :rules="[{ required: true, validator: validatePhone, trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.phone"></el-input>
      </el-form-item>

      <el-form-item label="专业擅长" prop="introduce" :rules="[
          { required: true, message: '请输入专业擅长', trigger: 'blur' },
        ]">
        <el-input resize="none" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入"
          v-model="form.introduce"></el-input>
      </el-form-item>

      <!--  <el-form-item
        label="职称"
        prop="position"
        :rules="[{ required: true, message: '请输入职称', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.position"></el-input>
      </el-form-item>
 -->
      <!--   <el-form-item
        label="所属科室"
        prop="department_id"
        :rules="[
          { required: true, message: '请选择所属科室', trigger: 'change' },
        ]"
      >
        <el-select
          v-model="form.department_id"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in departmentData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
 -->
      <!--    <el-form-item
        label="所属角色"
        prop="role_id"
        :rules="[
          { required: true, message: '请选择所属角色', trigger: 'change' },
        ]"
      >
        <el-select
          v-model="form.role_id"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in roleData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <!--  <el-form-item
        label="所属机构"
        prop="unit"
        :rules="[
          { required: true, message: '请输入所属机构', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.unit"></el-input>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { debounce } from "@/util";
import { ApiDoctorEdit, ApiDoctorAdd } from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
import { validatePhone } from "@/util/validate";
export default {
  props: ["roleData", "departmentData"],
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      validatePhone,
      optionsData: [
        { name: "男", code: "男" },
        { name: "女", code: "女" },
      ],
      levelArr: [
        { name: "普通", code: "普通" },
        { name: "中级", code: "中级" },
        { name: "高级", code: "高级" },
      ],

      oldform: {
        name: "",
        phone: "",
        level: "",
        icon: "",
        introduce: "",
        /*   username: "", */
        password: "",
        code: "",
        department: "",
        role: '',
        /*    unit: "京山市人民医院", */
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: "",
      imgsDefalut: [],
      roleArr: [{ name: '医师', code: '医师' }, { name: '护师', code: '护师' }]
    };
  },

  methods: {
    onUploadSuccess(path) {
      console.log("111", path);
      this.form.icon = path;
    },
    onUploadRemove() {
      this.form.icon = "";
    },
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;

            try {
              if (this.id) {
                await ApiDoctorEdit(this.form, this.id);
                this.$message.success("修改成功");
              } else {
                await ApiDoctorAdd(this.form);
                this.$message.success("添加成功");
                /* this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                }); */
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
      console.log("111111", row);
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }
      if (row) {
        this.id = row.id;
        this.title = "编辑人员";
        if (row.icon) this.imgsDefalut = [{ url: this.$path + row.icon }];
      } else {
        this.id = null;
        this.title = "添加人员";
      }
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
        this.$refs.ImgUploadPath.clear();
      });
    },
  },
};
</script>

<style></style>
